﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps.Controls
{
    public partial class FilStartUps_Comment : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["Skip"] = 0;
                GetComment(Convert.ToInt32(Request.QueryString["p"].Trim()));
            }
        }

        private void GetComment(int postidFK)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var comment = db_connect.Comments
                                     .Where(c => c.postidFK.Equals(postidFK)).ToList();
                ViewState["comment"] = comment;
                BindDataList(Convert.ToInt32(ViewState["Skip"]), postidFK);
                dlcomment.Visible = true;

                if (comment.Count > 10)
                {
                    lnkbNext.Visible = true;
                    lnkbPrev.Visible = true;
                    lnkbPrev.Enabled = false;
                }
                else
                {
                    lnkbNext.Visible = false;
                    lnkbPrev.Visible = false;
                }

            }
        }

        //Pagination
        private void BindDataList(int Skip, int postid)
        {
            int iPage = Skip + 10;
            int currPage = iPage / 10;
            double TotalPage;
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var comment = db_connect.Comments
                                    .Where(c => c.postidFK.Equals(postid)).ToList();


                TotalPage = Math.Ceiling((double)comment.Count / 10);

                var postarhieves_ = comment.Skip(Skip).Take(10)
                                        .OrderByDescending(c => c.commentidPK);

                //  (from c in db_connect.Comments
                //                   orderby c.commentidPK descending
                //                     select c).Skip(Skip).Take(10);




                if (iPage > comment.Count())
                {
                    lnkbNext.Enabled = false;
                }
                else
                {
                    lnkbNext.Enabled = true;
                }

                dlcomment.DataSource = postarhieves_;
                dlcomment.DataBind();

                litPageNumber.Text = "Page " + currPage.ToString().Trim() + " of " + (int)TotalPage;
            }
        }

        protected void lnkbNext_Click(object sender, EventArgs e)
        {
            lnkbPrev.Enabled = true;
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) + 10;
            BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["p"].Trim()));
        }

        protected void lnkbPrev_Click(object sender, EventArgs e)
        {
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) - 10;
            BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["p"].Trim()));
            if (Convert.ToInt32(ViewState["Skip"]) == 0)
            {
                lnkbPrev.Enabled = false;
            }
        }
        //protected void btncomment_Click(object sender, EventArgs e)
        //{
        //    using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
        //    {
        //        Comment comment = new Comment();
        //        comment.memberidFK = 1;
        //        comment.dateposted = DateTime.Now;
        //        comment.postdescription = txtcomment.Text.Trim();
        //        comment.postidFK = Convert.ToInt32(Request.QueryString["p"].Trim());
        //        db_connect.Comments.Add(comment);
        //        db_connect.SaveChanges();
        //    }
        //}

    }
}